#include "stdafx.h"
#include "version.h"
#include "CommonUtil.h"
#include "helper/SAppDir.h"

SOUI::SStringW GetProductMutexString( LPCTSTR pszInput ) {
    SOUI::SStringW strOut = pszInput;
    auto data = CommonUtil::ToUTF8(pszInput);
    data = CommonUtil::GetMD5(data);
    return CommonUtil::FromUTF8(data).c_str();
}

LPCTSTR GetProductMutexString() {
    static SOUI::SStringW s_strProductMutex;
    
    if ( s_strProductMutex.IsEmpty() ) {
        SOUI::SAppDir  appDir(nullptr);
        
        SOUI::SStringW dir = appDir.AppDir();
        dir += _T("XTrendSpeed");
        s_strProductMutex = GetProductMutexString(dir);
    }
    
    return s_strProductMutex;
}

LPCTSTR CheckBuildVersion( int iBuildCount ) {
    static SOUI::SStringW s_strBuildVersion;
    
    if ( s_strBuildVersion.IsEmpty() ) {
        s_strBuildVersion = _T(__DATE__);
        
        int iYear = 0;
        TCHAR szMonth[16] = {0};
        int iMonth = 0;
        int iDay = 0;
        int h, m, s;
        _stscanf(s_strBuildVersion, _T("%s %d %d %d:%d:%d"), szMonth, &iDay, &iYear, &h, &m, &s);
        _tcslwr(szMonth);
        
        LPCTSTR aMonths[] = {
            _T("jan")
            , _T("feb")
            , _T("mar")
            , _T("apr")
            , _T("may")
            , _T("jun")
            , _T("jul")
            , _T("aug")
            , _T("sep")
            , _T("oct")
            , _T("nov")
            , _T("dec")
        };
        for ( int i = 0; i < sizeof(aMonths) / sizeof(*aMonths); ++i ) {
            if ( _tcsicmp( aMonths[i], szMonth ) == 0 ) {
                iMonth = i + 1;
                break;
            }
        }
        s_strBuildVersion.Format( _T("%02d%02d%02d%0d"), (iYear % 100), iMonth, iDay, iBuildCount );
        s_strBuildVersion.GetLength();
    }
    
    return s_strBuildVersion;
}

SOUI::SStringW GetVersionName() {
    return _T("1.1.0");
}
